package org.lg.framework.protocol.http;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.Server;
import org.apache.catalina.Service;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.core.StandardEngine;
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.startup.Tomcat;
import org.springframework.context.annotation.Bean;

public class Test {

    String hostname = "localhost";
    int port = 8088;
    static Tomcat tomcat = new Tomcat();

    public void start(){
        System.out.println("mytomcat start1");
        tomcat = new Tomcat();
        Server server = tomcat.getServer();
        Service service = server.findService("Tomcat");
        //TOMCAT中 Engine是最高级别容器，Host是Engine的子容器，Context是Host的子容器，Wrapper是Context的子容器
        Connector connector = new Connector();
        connector.setPort(port);

        StandardEngine engine = new StandardEngine();
        engine.setDefaultHost(hostname);


        StandardHost host = new StandardHost();
        host.setName(hostname);

        StandardContext context = new StandardContext();
        context.addLifecycleListener(new Tomcat.FixContextListener());
        String contextPath = "/t2";
        context.setPath(contextPath);
        host.addChild(context);
        engine.addChild(host);

        service.setContainer(engine);
        service.addConnector(connector);//connector带端口

        //servlet配置
        tomcat.addServlet(contextPath,"aa",new TestServlet());
        context.addServletMappingDecoded("/cc","aa");
        try {
            tomcat.start();
            tomcat.getServer().await();//这一句和spring context 顺序的思考
        } catch (LifecycleException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        System.out.println("ttt");
        new Test().start();
    }
}
